import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager


@pytest.fixture()
def beforetest():
    # driver = webdriver.Chrome(service=Service("D:\pytest_ui_ocrdemo2\pytest_ui_ocrdemo\chromedriver.exe"))
    return "测试开始时的一些方法"
def browser():
    # 初始化浏览器（自动下载驱动）
    service = Service(ChromeDriverManager().install())
    options = webdriver.ChromeOptions()
    options.add_argument("--headless=new")  # 无头模式（可选）
    driver = webdriver.Chrome(service=service, options=options)
    driver.maximize_window()
    yield driver
    driver.quit()

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    # 失败时自动截图
    outcome = yield
    report = outcome.get_result()
    if report.when == "call" and report.failed:
        driver = item.funcargs.get("browser")
        if driver:
            driver.save_screenshot(f"reports/failure_{item.name}.png")

